import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store";
import Login from "@/view/admin/Login"
import Index from "@/view/dashboard/Index"
import StationList from "@/view/station/StationList";
import PileList from "@/view/pile/PileList";
import SpearList from "@/view/spear/SpearList";
import StationManager from "@/view/station/StationManager";
import BillRules from "../view/report/BillRules";
import TimeRules from "@/view/report/TimeRules";
import Transaction from "@/view/report/Transaction";
import MemberList from "@/view/report/MemberList";
import AdminList from "@/view/admin/AdminList"
import MenuList from "@/view/system/MenuList"
import MerchantList from "@/view/system/MerchantList"

Vue.use ( VueRouter )
const originalPush = VueRouter.prototype.push
//重复点击同一个路由
VueRouter.prototype.push = function push( location ) {
    return originalPush.call ( this, location ).catch ( err => err )
}

const router = new VueRouter ( {
    routes: [
        {path: '/', redirect: '/login'},
        {path: '/login', component: Login},
        {
            path: '/index', component: Index, redirect: '/stationList',
            children: [
                {path: '/stationList', name: '站点监控 / 实时运行', component: StationList},
                {path: '/stationManager', name: '站点监控 / 电站管理', component: StationManager},
                {path: '/pileList', name: '站点监控 / 充电桩', component: PileList},
                {path: '/spearList', name: '站点监控 / 充电枪', component: SpearList},
                {path: '/billRules', name: '统计报表 / 计费规则', component: BillRules},
                {path: '/rulesRateList', name: '统计报表 / 分时费率', component: TimeRules},
                {path: '/transaction', name: '统计报表 / 交易记录', component: Transaction},
                {path: '/memberList', name: '统计报表 / 会员管理', component: MemberList},
                {path: '/admin', name: '人员管理 / 用户列表', component: AdminList},
                {path: '/menu', name: '系统管理 / 菜单列表', component: MenuList},
                {path: '/merchant', name: '系统管理 / 商户列表', component: MerchantList},
            ]
        }
    ]
} )

router.beforeEach ( ( to, from, next ) => {
    const token = window.sessionStorage.getItem ( 'token' )
    store.dispatch ( 'asyncSetRouterName', {
        routerName: to.name
    } )
    next ()
} )

export default router
